﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Mvc.Controllers;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.ItemList.Repositories;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.ItemList.Models.Entities;
using MvcEngine.Mvc;
using MvcEngine.ItemList.Components.Extensions;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core.Repositories;
using Entities = MvcEngine.ItemList.Models.Entities;
using MvcEngine.Mvc.Filters;

namespace MvcEngine.ItemList.Controllers
{
    public class ItemListController : SubController
    {
        public MvcEngineModel Index(Guid pageId)
        {
            var list = IoC.Resolve<IItemListRepository>().GetAll();
            return new MvcEngineModelList<Entities.ItemList>() { List = list };
        }

        public MvcEngineModel Details(Guid id)
        {
            var item = IoC.Resolve<IItemListRepository>().Get(id);
            return new MvcEngineModelItem<Entities.ItemList>() { Item = item };
        }

        [AcceptVerbs(HttpVerbs.Get)]
        [Permission(ItemListPermissions.EditItemList)]
        public MvcEngineModel Edit(Guid id)
        {
            var item = IoC.Resolve<IItemListRepository>().Get(id);
            return new MvcEngineModelItem<Entities.ItemList>() { Item = item };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ActionName("Edit")]
        [ValidateInput(false)]
        [Permission(ItemListPermissions.EditItemList)]
        [AntiForgeryValidationFilter]
        public object Save(Guid id)
        {
            var data = IoC.Resolve<Entities.ItemList>();

            if (TryUpdateModel<Entities.ItemList>(data))
            {
                data.Id = id;
                IoC.Resolve<IItemListRepository>().Update(data);

                return Redirect(Url.ItemListDetails(id));
            }
            else
            {
                return Edit(id);
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        [Permission(ItemListPermissions.NewItemList)]
        public MvcEngineModel New()
        {
            var item = IoC.Resolve<Entities.ItemList>();
            return new MvcEngineModelItem<Entities.ItemList>() { Item = item };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ActionName("New")]
        [ValidateInput(false)]
        [Permission(ItemListPermissions.NewItemList)]
        [AntiForgeryValidationFilter]
        public object SaveNew(Guid pageId)
        {
            var data = IoC.Resolve<Entities.ItemList>();

            if (TryUpdateModel<Entities.ItemList>(data))
            {
                data.Id = Guid.NewGuid();
                data.PageId = pageId;
                IoC.Resolve<IItemListRepository>().Add(data);

                return Redirect(Url.ItemListDetails(data.Id));
            }
            else
            {
                return New();
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [Permission(ItemListPermissions.DeleteItemList)]
        [AntiForgeryValidationFilter]
        public ActionResult Delete(Guid id)
        {
            IoC.Resolve<IItemListRepository>().Delete(id);

            return Redirect(Url.Page());
        }
    }
}
